<template>
	<view class="defaultStyles">
	</view>
</template>

<script lang="uts">
	/**
   * 引用 Android 系统库
   */
	import CpuAdView from 'com.baidu.mobads.sdk.api.CpuAdView'
	import CPUWebAdRequestParam from 'com.baidu.mobads.sdk.api.CPUWebAdRequestParam'
	import CpuLpFontSize from 'com.baidu.mobads.sdk.api.CpuLpFontSize'
	import UUID from 'java.util.UUID';
	import Log from 'android.util.Log';
	
	class MyCpuAdViewInternalStatusListener implements CpuAdView.CpuAdViewInternalStatusListener {

		override loadDataError(message : string) {//网页加载错误
			Log.e("AdView", message);
		}
		override onAdClick() {
			Log.d("AdView", "onAdClick");
		}
		override onAdImpression(impressionAdNums : string) {
			Log.d("AdView", "impressionAdNums =  " + impressionAdNums);
		}
		override onContentClick() {
			Log.d("AdView", "onContentClick")
		}
		override onContentImpression(impressionContentNums : string) {
			Log.d("AdView", "impressionContentNums =  " + impressionContentNums);
		}
		override onExitLp() {
			Log.d("AdView", "onExitLp")
		}
		override onLpContentStatus(map : MutableMap<String, Any>) {
			Log.d("AdView", "onLpContentStatus:" + map)
		}
	}
	export default {
		name: "cpu-ad-view",//组件名称，也就是开发者使用的标签
		props: {
			"appsid": {
				type: String,
				default: ""
			},
			"channelId": {
				type: Int,
				default: 0
			},
			"subchannelId": {
				type: String,
				default: ""
			},
			"isDarkMode": {
				type: Boolean,
				default: false,
			},
			"localChannel": {
				type: String,
				default: "北京"
			}
		},
		/**
		 * 组件内部变量声明
		 */
		data() {
			return {}
		},
		/**
		 * 规则：如果没有配置expose，则methods中的方法均对外暴露，如果配置了expose，则以expose的配置为准向外暴露
		 * ['publicMethod'] 含义为：只有 `publicMethod` 在实例上可用
		 */
		expose: ['requestData'],
		methods: {
			/**
			 * 对外公开的组件方法
			 * uni-app中调用示例：
			 * this.$refs["组件ref"].requestData();
			 */
			requestData() {
				this.$el?.requestData()//刷新方法
			},
		},
		created() {
		},
		NVLoad() : CpuAdView {
			let outerId = UUID.randomUUID().toString()
				.replace("-", "")
				.substring(0, 16);
			let cpuWebAdRequestParam = new CPUWebAdRequestParam.Builder()
				.setLpFontSize(CpuLpFontSize.REGULAR)
				.setLpDarkMode(this.isDarkMode)
				.setCityIfLocalChannel(this.localChannel) //媒体如果选择本地频道，配置该参数
				.setCustomUserId(outerId)
				.setSubChannelId(this.subchannelId) // 登录百青藤-应用合作-合作管理-内容联盟-内容联盟接入-内容场景ID列获取，有 则必须填写，无则不填
				.build();
			let cpuAdView = new CpuAdView($androidContext!, this.appsid, this.channelId, cpuWebAdRequestParam, new MyCpuAdViewInternalStatusListener());
			console.log("NVLoad" + cpuAdView.getHeight())
			return cpuAdView;
		},
		/**
		 * [可选实现] 原生View已创建
		 */
		NVLoaded() {
			console.log("NVLoaded")
			this.$el?.requestData()
		},
		/**
		 * [可选实现] 原生View将释放
		 */
		NVBeforeUnload() {
			console.log("NVBeforeUnload")
		},
		/**
		 * [可选实现] 原生View已释放，这里可以做释放View之后的操作
		 */
		NVUnloaded() {
			this.$el?.onDestroy();
			console.log("NVUnloaded")
		},
		/**
		 * [可选实现] 组件销毁
		 */
		unmounted() {
			console.log("unmounted")
		},
	}
</script>

<style>
	.defaultStyles {
	    width: 750rpx;
	    height: 240rpx;
	}
</style>